package cn.hdu.liang.ch6;

import cn.hdu.liang.ch4.Dish;

import java.util.Optional;

import static cn.hdu.liang.ch4.Dish.menu;
import static java.util.stream.Collectors.reducing;

public class Reducing {
    public static void main(String[] args) {
        Integer totalCa = menu.stream().collect(reducing(0, Dish::getCalories, (i, j) -> i + j));
        System.out.println(totalCa.intValue());

        Optional<Dish> mostCaDish = menu.stream()
                .collect(reducing((d1, d2) -> d1.getCalories() > d2.getCalories() ? d1 : d2));
        System.out.println(mostCaDish.get());

        Integer totalCa2 = menu.stream()
                .collect(reducing(0, Dish::getCalories, Integer::sum));
        System.out.println(totalCa2);

        Integer totalCa3 = menu.stream().map(Dish::getCalories).reduce(Integer::sum).get();
        System.out.println(totalCa3);

        int totalCa4 = menu.stream().mapToInt(Dish::getCalories).sum();
        System.out.println(totalCa4);
    }
}
